var proType = null;
var status = null;
var cycle = null;
var pageNo = null;
var totalPage = null;

$(function(){
	//初始化加载数据
	initList();
});

function initList(){
	//加载产品列表
	products();
	//加载页码
	if(totalPage == 0){
		$("#page").html('<li>暂无数据</li>');
	}else{
		$("#page").Page({
			totalPages : totalPage,//分页总数
			liNums : 7,//分页的数字按钮数(建议取奇数)
			activeClass : 'activP', //active 类样式定义
			    hasFirstPage: true,//whether has first button
			    hasLastPage: true,//whether has last button
			    hasPrv: true,//whether has prev button
			    hasNext: true,//whether has next button
				callBack : function(page){
					pageNo = page;
					products();
				}
		});
	}
}

function products(){
	$.ajax({
		url:g_requestContextPath + "/p/products",
		type:"post",
		dataType:"json",
		async: false, 
		data:{
			"proType":proType,
			"status":status,
			"cycle":cycle,
			"pageNo":pageNo
			},
		success:function(data){
			
			var products = data.list;
			totalPage = Math.ceil(parseInt(data.totalRow) / 6);
			
			var investCalculate = {"full":"满标计息","invest":"每日计息"};
			var units = {"day":"天","month":"月","week":"周","year":"年"};
			
			var listHtml ="";
			for(var i = 0;i < products.length; i++){
				listHtml += '<li>';
				listHtml += '<div class="u-tetle">';
				if(products[i].proType == 'NEW_INVESTOR'){
					listHtml += '<i class="icon">新</i>';
				}else{
					listHtml += '<i class="icon1">定</i>';
				}
				listHtml += '<h3><a class="name" href="'+g_requestContextPath+'/p/detial/'+products[i].pid+'">'+products[i].title+'</a> <p>'+products[i].contract+'</p></h3>';
				listHtml += '</div>';
				listHtml += '<div class="u-listleft fl">';
				listHtml += '<div class="u-iactivity"><i class="u-icon"><img src="'+g_requestContextPath+'/images/listicon.png"></i></div>';
				/*<span>首单送红包 </span><span>尾单返现20元 </span><span>小米活动</span>*/
				listHtml += '</div>';
				listHtml += '<div class="u-listright fr">';
				/*listHtml += '<div class="u-rate u-item"><p><i>'+(products[i].investRate - products[i].awardRate)+'</i>%+'+products[i].awardRate+'%</p><span>预期年化利率</span></div>';*/
				listHtml += '<div class="u-rate u-item"><p><i>'+changeRateShow(products[i].investRate,products[i].cycle,products[i].unit)+'%</p><span>预期年化利率</span></div>';
				listHtml += '<div class="u-period u-item"><p><i>'+products[i].cycle+'</i>'+units[products[i].unit]+'</p><span>投资期限 </span></div>';
				listHtml += '<div class="u-progress u-item">';
				listHtml += '<div class="circle_container" id="circle_borrow_0" data-progress="0">';
				listHtml += '<svg viewBox="0 0 100 50">';
				listHtml += '<path d="M 50,50 m -48,0 a 48,48 0 1 1 96,0" stroke="#eaeaea" stroke-width="2" fill-opacity="0"></path>';
				listHtml += '<path id="path" d="M 50,50 m -48,0 a 48,48 0 1 1 96,0" stroke="rgb(236,195,70)" stroke-width="2" fill-opacity="0" style="stroke-dasharray: '+progressBar(products[i].yesAount,products[i].amount)+'px, 150.818px; stroke-dashoffset: 15.818px;"></path>';
				listHtml += '</svg>';
				listHtml += '<div class="progressbar-text" style="position: absolute; left: 50%; top: auto; padding: 0px; margin: 0px; transform: translate(-50%, 50%); color: rgb(255, 158, 0); bottom: 0px; font-family: Raleway, Helvetica, sans-serif; font-size: 2rem;"></div></div>';
				listHtml += '<p><i>'+thousandBitSeparator(parseInt(products[i].amount/100-products[i].yesAount/100))+'</i> 元</p><span>可投金额 </span></div>';
				listHtml += '<div class="">';
				if(products[i].status == '2'){
					listHtml += '<a href="'+g_requestContextPath+'/p/detial/'+products[i].pid+'" class="u-but">立即投资</a>';
				}else if(products[i].status == '3'){
					listHtml += '<a class="u-but u-red">复审中</a>';
				}else if(products[i].status == '4' || products[i].status == '5'){
					listHtml += '<a class="u-but u-yellow">计息中</a>';
				}else if(products[i].status == '6'){
					listHtml += '<a class="u-but disabled">已还款</a>';
				}
				
				listHtml +='</div></div></li>';
				
			}
			$(".pros-list").html(listHtml);
		}
	});
}

//产品类型
$(".proTypes a").click(function(){
	$(this).addClass("current").siblings().removeClass("current");
	pageNo = 0;
	proType = $(this).attr("data-msg-value");
	initList();
});
//投资状态
$(".proStatus a").click(function(){
	$(this).addClass("current").siblings().removeClass("current");
	pageNo = 0;
	status = $(this).attr("data-msg-value");
	initList();
});
//项目周期
$(".proTime a").click(function(){
	$(this).addClass("current").siblings().removeClass("current");
	pageNo = 0;
	cycle = $(this).attr("data-msg-value");
	initList();
});

/**
 * 投资进度条
 * @param yesAmount
 * @param amount
 * @returns
 */
function progressBar(yesAmount,amount){
	var ratio = parseFloat(yesAmount)/parseFloat(amount);
	var result = parseFloat(150.818)*parseFloat(ratio);
	return Math.round(result*100)/100 + 16;
}

/**
 * 修改利率显示方式
 * @param rate
 * @param cycle
 * @param unit
 * @returns
 */
function changeRateShow(rate,cycle,unit){
	var rateShow = rate+"</i>";
	var rateChanged = 0;
	if(unit == 'day'){
		if(cycle >= 30 && cycle < 90){
			if(rate > 8){
				rateChanged = parseFloat(new Number(rate)-8).toFixed(2);
				rateShow = "8</i>%+"+rateChanged+"";
			}
		}else if(cycle >= 90){
			if(rate > 10){
				rateChanged = parseFloat(new Number(rate)-10).toFixed(2);
				rateShow = "10</i>%+"+rateChanged+"";
			}
		}
	}else if(unit == 'month'){
		if(cycle >= 1 && cycle < 3){
			if(rate > 8){
				rateChanged = parseFloat(new Number(rate)-8).toFixed(2);
				rateShow = "8</i>%+"+rateChanged+"";
			}
		}else if(cycle >= 3){
			if(rate > 10){
				rateChanged = parseFloat(new Number(rate)-10).toFixed(2);
				rateShow = "10</i>%+"+rateChanged+"";
			}
		}
	}
	return rateShow;
}
